package com.yenlien.traceyun.plant.utils;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.common.utils.HttpUtil;
import com.yenlien.traceyun.common.utils.LoggerUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-12-30
 * @description:
 **/
public class ToolsUtil {

    /**
     * ip解析省市县
     */
//    private  static String IP_URL = "http://api.map.baidu.com/location/ip?ak=hjnh46U3CnCqL9xg5KkLeRX3PyPoQzjB&ip=";
    private static String IP_URL = "http://api.map.baidu.com/location/ip?ak=cq97dH2kOoAnfQ39YXryuBGTAsmnkQAm&ip=";

    /**
     * 地址信息解析省市县
     */
    private static String ADDRESS_URL = "http://api.map.baidu.com/address_analyzer/v1?ak=hjnh46U3CnCqL9xg5KkLeRX3PyPoQzjB&address=";

    /**
     * 天气接口
     */
    private static String WEATHER_URL = "http://api.map.baidu.com/weather/v1/?data_type=all&ak=hjnh46U3CnCqL9xg5KkLeRX3PyPoQzjB&district_id=";


    /**
     * 获取ip地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        Object o = request.getParameter("_remote_ip");
        if (null != o && !"".equals(o.toString())) {
            return o.toString();
        }
        long start = System.currentTimeMillis();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("REMOTE-HOST");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 解析ip
     *
     * @param ip
     * @return
     */
    public static String parseIp(String ip) {
        if (ip.contains(",")) {
            ip = ip.split(",")[0];
        }
        String result = HttpUtil.get(IP_URL + ip);
        if (result != null) {
            JSONObject json = JSONObject.parseObject(result);
            if (json.containsKey("status")) {
                if (json.getString("status").equals("0")) {
                    String address = json.getString("address");
                    if (address.contains("|")) {
                        String[] split = address.split("\\|");//  \\| 转移后就是 |
                        return split[1] + split[2];
                    }
                    return json.getString("address");
                } else {
                    LoggerUtil.logError("ip[" + ip + "]解析错误:" + result);
                    return "未知";
                }
            } else {
                LoggerUtil.logError("ip[" + ip + "]解析错误:" + result);
                return "未知";
            }
        } else {
            LoggerUtil.logError("ip[" + ip + "]解析错误:" + result);
            return "未知";
        }
    }

    /**
     * 解析ip
     *
     * @param address
     * @return
     */
    public static String parseAddress(String address) {
        String result = HttpUtil.get(ADDRESS_URL + address);
        if (result != null) {
            JSONObject json = JSONObject.parseObject(result);
            if (json.containsKey("status")) {
                if (json.getString("status").equals("0")) {
                    return json.getString("address");
                } else {
                    LoggerUtil.logError("地址[" + address + "]解析错误:" + result);
                    return null;
                }
            } else {
                LoggerUtil.logError("地址[" + address + "]解析错误:" + result);
                return null;
            }
        } else {
            LoggerUtil.logError("地址[" + address + "]解析错误:" + result);
            return null;
        }
    }

    /**
     * 解析ip
     *
     * @param areaCode
     * @return
     */
    public static String parseWeather(String areaCode) {

        String result = HttpUtil.get(WEATHER_URL + areaCode);
        if (result != null) {
            JSONObject json = JSONObject.parseObject(result);
            if (json.containsKey("status")) {
                if (json.getString("status").equals("0")) {
                    return json.getString("result");
                } else {
                    LoggerUtil.logError("areaCode[" + areaCode + "]解析错误:" + result);
                    return null;
                }
            } else {
                LoggerUtil.logError("areaCode[" + areaCode + "]解析错误:" + result);
                return null;
            }
        } else {
            LoggerUtil.logError("areaCode[" + areaCode + "]解析错误:" + result);
            return null;
        }
    }
}
